package com.design.observer;


import com.design.strategy.Strategy;
import com.design.strategy.StrategyContext;
import com.design.strategy.StrategyService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 策略模式bean监听器
 *
 * @author Zee
 * @date 2021年8月27日
 */
@Component
public class ObserverListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(Observer.class);
        ObserverSubject observerSubject = event.getApplicationContext().getBean(ObserverSubject.class);
        beans.forEach((name, bean) ->
                observerSubject.registerObserver((ObserverComponent) bean)
        );
    }
}
